using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace NUnitTestHelper
{
    class NUnitResultsParser
    {
        private int m_FailuresCount = 0;
        private int m_TotalCount = 0;
        private int m_IgnoreCount = 0;

        public int FailuresCount
        {
            get { return m_FailuresCount; }
            set { m_FailuresCount = value; }
        }
        public int TotalCount
        {
            get { return m_TotalCount; }
            set { m_TotalCount = value; }
        }
        public int IgnoreCount
        {
            get { return m_IgnoreCount; }
            set { m_IgnoreCount = value; }
        }

        private XPathNavigator m_TestResultsNav = null;

        /// <summary>
        /// Constructor for NUnitResultsParser
        /// </summary>
        /// <param name="sNUnitTestResultFilePath">Path the NUnit Test Results file</param>
        public NUnitResultsParser(string sNUnitTestResultFilePath)
        {
            // Create a Navigator for NUnit's TestResult.xml File
            XPathDocument doc = new XPathDocument(sNUnitTestResultFilePath);

            m_TestResultsNav = doc.CreateNavigator();
        }

        /// <summary>
        /// Parsers the NUnit Test Results file and
        /// returns the failures in a string
        /// </summary>
        /// <returns></returns>
        public string GetTestResults()
        {
            StringBuilder Results = new StringBuilder(200);

            try
            {
                if (m_TestResultsNav != null)
                {
                    XPathNodeIterator iterator = m_TestResultsNav.Select("test-results");

                    if (iterator.MoveNext() == false)
                        return (String.Empty);

					Trace.WriteLine("\n************************************");

                    m_TotalCount = int.Parse(iterator.Current.GetAttribute("total", ""));
                    Results.AppendLine("NUnit Test Count: " + m_TotalCount);
                    Trace.WriteLine("NUnit Test Count: " + m_TotalCount);

                    m_FailuresCount = int.Parse(iterator.Current.GetAttribute("failures", ""));
                    Results.AppendLine("NUnit Failures: " + m_FailuresCount);
                    Trace.WriteLine("NUnit Failures: " + m_FailuresCount);

                    m_IgnoreCount = int.Parse(iterator.Current.GetAttribute("not-run", ""));
                    Results.AppendLine("NUnit Tests Not Run Count: " + m_IgnoreCount);
                    Trace.WriteLine("NUnit Tests Not Run Count: " + m_IgnoreCount);

                    string sTestDate = iterator.Current.GetAttribute("date", "");
                    Results.AppendLine("NUnit Test Date: " + sTestDate);
                    Trace.WriteLine("NUnit Test Date: " + sTestDate);

                    string sTestTime = iterator.Current.GetAttribute("time", "");
                    Results.AppendLine("NUnit Test Time: " + sTestTime);
                    Trace.WriteLine("NUnit Test Time: " + sTestTime);


                    iterator = m_TestResultsNav.Select(".//test-case[@success='False']");
                    while (iterator.MoveNext())
                    {
                        string sTestCaseName = iterator.Current.GetAttribute("name", "");
                        Results.AppendLine("\nNUnit Failed Test Case Name: " + sTestCaseName);
                        Trace.WriteLine("\nNUnit Failed Test Case Name: " + sTestCaseName);

                        XPathNodeIterator msgIterator = iterator.Current.SelectDescendants("message", "", false);

                        if (msgIterator.MoveNext())
                        {
                            string sErrorMessage = msgIterator.Current.Value;
                            Results.AppendLine("NUnit Test Error Message: " + sErrorMessage);
                            Trace.WriteLine("NUnit Test Error Message: " + sErrorMessage);

                            //if (msgIterator.MoveNext())
                            //{
                            //    string sStackTrace = msgIterator.Current.Value;
                            //    Results.AppendLine();
                            //    Trace.WriteLine("NUnit Test Stack Trace: " + sErrorMessage);
                            //}
                        }
                    }

					Trace.WriteLine("************************************\n");
                }
            }
            catch (Exception Ex)
            {
                string sError = Ex.Message;
            }

            return (Results.ToString());
        }
    }
}
